package net.groupme.utils.sql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import net.groupme.exceptions.IDNotFoundException;
import net.groupme.model.dao.rdf.StatementDAOImpl;
import net.groupme.model.rdf.Statement;
import net.groupme.model.rdf.StatementImpl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.object.MappingSqlQuery;

public class GetStatementsQuery extends MappingSqlQuery{
	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());
	
	private DataSource datasource;
	private String columnName;
	public GetStatementsQuery(DataSource ds, String query, String columnName) {
        super(ds, query);
        this.datasource = ds;
        this.columnName = columnName;
        compile();
    }

    protected Object mapRow(ResultSet rs, int rowNum) throws SQLException {
        List<Statement> statements = new ArrayList<Statement>();
         while (!rs.isAfterLast()){
        	 Statement statement;
			try {
				statement = new StatementImpl(new StatementDAOImpl(datasource, rs.getString(columnName)));
				statements.add(statement);
			} catch (IDNotFoundException e) {
				logger.debug(e.getMessage());
			}
        	 rs.next();
        }
    	return statements; 
    }
}
